package com.wanjia.estate.service.bdservice.ydzfkplxdsprieservice;

import com.wanjia.estate.pojo.entity.Ydzfkplxd;
import com.wanjia.estate.properties.FileTemplateProperties;
import com.wanjia.estate.utils.MD5Util;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

@Service
public class ExcelReaderWriterYdzfkplxdService {

    @Autowired
    private FileTemplateProperties fileTemplateProperties;

    public String ExcelReaderWriter(Ydzfkplxd ydzfkplxd,String folder) {
        String md5FileName = MD5Util.getMD5(ydzfkplxd.getSjbh());
        try {
            FileInputStream inputStream = new FileInputStream(fileTemplateProperties.getZjsbdcdjydzfkplxdpath());
            Workbook workbook = WorkbookFactory.create(inputStream);
            Sheet sheet = workbook.getSheetAt(0);
            Row row2 = sheet.getRow(1);
            row2.getCell(1).setCellValue(ydzfkplxd.getJfsj());
            row2.getCell(5).setCellValue(ydzfkplxd.getSjbh());

            Row row3 = sheet.getRow(2);
            row3.getCell(5).setCellValue(ydzfkplxd.getBm());

            Row row4 = sheet.getRow(3);
            row4.getCell(2).setCellValue(ydzfkplxd.getJkdwqc());
            row4.getCell(7).setCellValue(ydzfkplxd.getSkdwzhmc());

            Row row5 = sheet.getRow(4);
            row5.getCell(2).setCellValue(ydzfkplxd.getJkdwzh());
            row5.getCell(7).setCellValue(ydzfkplxd.getSkdwzh());

            Row row6 = sheet.getRow(5);
            row6.getCell(2).setCellValue(ydzfkplxd.getJkdwkhyh());
            row6.getCell(7).setCellValue(ydzfkplxd.getSkdwkhyh());

            Row row8 = sheet.getRow(7);
            row8.getCell(0).setCellValue(ydzfkplxd.getXm());
            row8.getCell(2).setCellValue(ydzfkplxd.getDm());
            row8.getCell(3).setCellValue(ydzfkplxd.getSl());
            row8.getCell(4).setCellValue(ydzfkplxd.getBiaozhun());
            row8.getCell(6).setCellValue(ydzfkplxd.getJe());

            Row row9 = sheet.getRow(8);
            row9.getCell(2).setCellValue(ydzfkplxd.getFczl());

            Row row10 = sheet.getRow(9);
            row10.getCell(2).setCellValue(ydzfkplxd.getHjrmb());

            Row row11 = sheet.getRow(10);
            row11.getCell(2).setCellValue(ydzfkplxd.getBeizhu());
            row11.getCell(7).setCellValue(ydzfkplxd.getJszt());

            Row row12 = sheet.getRow(11);
            row12.getCell(1).setCellValue(ydzfkplxd.getQlr());
            row12.getCell(7).setCellValue(ydzfkplxd.getBlr());

            Row row13 = sheet.getRow(12);
            row13.getCell(1).setCellValue(ydzfkplxd.getYwr());

            FileOutputStream outputStream = new FileOutputStream(folder+"/"+md5FileName + "kplxd.xlsx");
            workbook.write(outputStream);
            workbook.close();
            outputStream.close();
        } catch (IOException | InvalidFormatException e) {
            e.printStackTrace();
        }
        File file = new File(folder+"/"+md5FileName + "kplxd.xlsx");
        return file.getPath();
    }
}
